package com.library.web.controller.library;

import com.library.common.annotation.Log;
import com.library.common.core.controller.BaseController;
import com.library.common.core.domain.AjaxResult;
import com.library.common.core.page.TableDataInfo;
import com.library.common.enums.BusinessType;
import com.library.common.utils.poi.ExcelUtil;
import com.library.domain.BoIntegralHistory;
import com.library.domain.BoMembers;
import com.library.service.IBoIntegralHistoryService;
import com.library.service.IBoMembersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 积分调整记录 信息操作处理
 *
 * @author wanghongjie
 * @date 2020-01-18
 */
@RestController
@RequestMapping("/library/boIntegralHistory")
public class BoIntegralHistoryController extends BaseController {
    private String prefix = "business/library/boIntegralHistory";

    @Autowired
    private IBoIntegralHistoryService boIntegralHistoryService;
    /**
     * 会员Service
     */
    @Autowired
    private IBoMembersService boMembersService;


    /**
     * 查询积分调整记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:history:list')")
    @GetMapping("/list")
    public TableDataInfo list(BoIntegralHistory boIntegralHistory) {
        startPage();
        List<BoIntegralHistory> list = boIntegralHistoryService.selectBoIntegralHistoryList(boIntegralHistory);
        return getDataTable(list);
    }

    /**
     * 导出积分调整记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:history:export')")
    @Log(title = "积分调整记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BoIntegralHistory boIntegralHistory) {
        List<BoIntegralHistory> list = boIntegralHistoryService.selectBoIntegralHistoryList(boIntegralHistory);
        ExcelUtil<BoIntegralHistory> util = new ExcelUtil<BoIntegralHistory>(BoIntegralHistory.class);
        util.exportExcel(response, list, "积分调整记录数据");
    }

    /**
     * 获取积分调整记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:history:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(boIntegralHistoryService.selectBoIntegralHistoryById(id));
    }

    /**
     * 新增积分调整记录
     */
    @PreAuthorize("@ss.hasPermi('system:history:add')")
    @Log(title = "积分调整记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BoIntegralHistory boIntegralHistory) {
        boIntegralHistory.setUserId(getUsername());
        if (null == boIntegralHistory.getMembersId()) {

        }
        //获取用户现有积分
        BoMembers members = boMembersService.selectBoMembersById(boIntegralHistory.getMembersId());
        int beforeAdjustNumber = 0;
        if (null != members.getAvailableIntegral()) {
            beforeAdjustNumber = members.getAvailableIntegral();
        }
        //保存调整前积分
        boIntegralHistory.setBeforeAdjust(beforeAdjustNumber);
        //计算保存调整后积分
        int afterAdjustNumber = beforeAdjustNumber + boIntegralHistory.getAdjustNumber();
        boIntegralHistory.setAfterAdjust(afterAdjustNumber);
        //更新用户可用积分
        members.setAvailableIntegral(afterAdjustNumber);
        int integralLevel = 0;
        if (null != members.getIntegralLevel()) {
            integralLevel = members.getIntegralLevel();
        }
        if (boIntegralHistory.getAdjustNumber() > 0) {
            members.setIntegralLevel(integralLevel + boIntegralHistory.getAdjustNumber());
        }
        boMembersService.updateBoMembers(members);
        return toAjax(boIntegralHistoryService.insertBoIntegralHistory(boIntegralHistory));
    }

    /**
     * 修改积分调整记录
     */
    @PreAuthorize("@ss.hasPermi('system:history:edit')")
    @Log(title = "积分调整记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BoIntegralHistory boIntegralHistory) {
        return toAjax(boIntegralHistoryService.updateBoIntegralHistory(boIntegralHistory));
    }

    /**
     * 删除积分调整记录
     */
    @PreAuthorize("@ss.hasPermi('system:history:remove')")
    @Log(title = "积分调整记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(boIntegralHistoryService.deleteBoIntegralHistoryByIds(ids));
    }

}
